package Filtro;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import constantes.ComandosNavegacion;


public class FiltroAutenticacion implements PhaseListener
{

	@Override
	public void afterPhase(PhaseEvent arg0) 
	{

	}

	@Override
	public void beforePhase(PhaseEvent arg0)
	{
		FacesContext faces = arg0.getFacesContext();
		String pagina = faces.getViewRoot().getViewId();
		ExternalContext contextoExterno = faces.getExternalContext();
		
		if(!pagina.equals("/index.jsp"))
		{
			if(contextoExterno.getSessionMap().get("Usuario")==null)
			{				
				faces.getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, ComandosNavegacion.COMANDONAVEGACIONINDEX);
			}		
		}

	}

	public PhaseId getPhaseId() 
	{
		return PhaseId.RENDER_RESPONSE;
	}

}
